﻿using EkStudio.com.bean.xml.BillModule;
using EkStudio.com.common.unit;
using EkStudio.com.ui.unitbill.property.Event;
using EkStudio.com.ui.unitbill.property.factory;
using EkStudio.Com.UI.Interface;
using System;
using System.ComponentModel;
using System.Reflection;

namespace EkStudio.com.ui.unitbill.property
{
    public class BillUiGroupToProperty:ObjectToProperty
    {



        private BillUiGroupItems item = null;
        private BillUiGroupToProperty[] array = null;

        log4net.ILog logger = log4net.LogManager.GetLogger(typeof(BillUiGroupToProperty));


        public event KEventHandler DataChange = null;

        public BillUiGroupToProperty(BillUiGroupItems bean)
        {
            this.item = bean;
        }

        public bool IsMultiple()
        {
            return array != null;
        }

        public BillUiGroupToProperty(BillUiGroupToProperty[] _array)
        {
            this.array = _array;
        }
        [DescriptionAttribute(ULabel.PROPERTY_TABLE_KEY_DES)]
        public string Key
        {
            get { return item!=null?item.Key:"";}
            set {
                if (item != null)
                    item.Key = value;
                else
                    SetValue("Key", value);
                
            }
        }
        [DescriptionAttribute(ULabel.PROPERTY_TABLE_CAPTION_DES)]
        public string Caption
        {
            get { return item != null ? item.Caption:""; }
            set {
                if (item != null)
                {
                    item.Caption = value;
                }
                else
                {
                    SetValue("Caption", value);
                }
                
            }
        }
        
        public bool IsTable
        {
            get
            {
                return item!=null?item.Table:GetBoolValue("IsTable");
            }

            set
            {
                if(item!=null)item.Table = value;
                if (DataChange != null)
                {
                    DataChange(this, new PropertyDataEvent("IsTable", value));
                }
            }
        }
        
        public string TableName
        {
            get
            {
                return item!=null?item.TableName:"";
            }

            set
            {
                if(item!=null)item.TableName = value;
                
            }
        }
        [DescriptionAttribute(ULabel.PROPERTY_TABLE_ShowModel_DES)]
        public string ShowModel
        {
            get
            {
                return item!=null?item.ShowModel: GetStringValue("ShowModel");
            }

            set
            {
                if (item != null) item.ShowModel = value;
                else
                    SetValue("ShowModel", value);

                
            }
        }
        
        public bool Visible
        {
            get
            {
                return item!=null?item.Visible:GetBoolValue("Visible");
            }

            set
            {
                if (item != null)
                {
                    item.Visible = value;
                }
                else
                {
                    SetValue("Visible", value);
                }
                

            }
        }
        
        public bool Enable
        {
            get
            {
                return item!=null?item.Enable:GetBoolValue("Enable");
            }

            set
            {
                if (item != null)
                {
                    item.Enable = value;
                }
                else
                {
                    SetValue("Enable", value);
                }
              

            }
        }
        
        public bool ReadOnly
        {
            get
            {
                return item!=null?item.ReadOnly:GetBoolValue("ReadOnly");
            }

            set
            {
                if (item != null)
                    item.ReadOnly = value;
                else
                    SetValue("ReadOnly", value);
                
            }
        }
        [DescriptionAttribute(ULabel.PROPERTY_TABLE_Expanded_DES)]
        public bool Expanded
        {
            get
            {
                return item!=null?item.Expanded:GetBoolValue("Expanded");
            }

            set
            {
                if (item != null)
                    item.Expanded = value;
                else
                    SetValue("Expanded", value);

                
            }
        }
        [DescriptionAttribute(ULabel.PROPERTY_TABLE_ExpandField_DES)]
        public string ExpandField
        {
            get
            {
                return item!=null?item.ExpandField:"";
            }

            set
            {
                if (item != null)
                    item.ExpandField = value;
                else
                    SetValue("ExpandField", value);
                
            }
        }
        [DescriptionAttribute(ULabel.PROPERTY_TABLE_HasPopMenu_DES)]
        public bool HasPopMenu
        {
            get
            {
                return item!=null?item.HasPopMenu:GetBoolValue("HasPopMenu");
            }

            set
            {
                if (item != null)
                    item.HasPopMenu = value;
                else
                    SetValue("HasPopMenu", value);
               
            }
        }
        [DescriptionAttribute(ULabel.PROPERTY_TABLE_ShowQuery_DES)]
        public bool ShowQuery
        {
            get
            {
                return item!=null?item.ShowQuery:GetBoolValue("ShowQuery");
            }

            set
            {
                if (item != null)
                {
                    item.ShowQuery = value;
                }
                else
                {
                    SetValue("ShowQuery", value);
                }
                
              
            }
        }
        [DescriptionAttribute(ULabel.PROPERTY_TABLE_SchemeDefaut_DES)]
        public bool SchemeDefaut
        {
            get
            {
                return item!=null?item.SchemeDefaut:GetBoolValue("SchemeDefaut");
            }

            set
            {
                item.SchemeDefaut = value;
                
            }
        }
        
        public int Columns
        {
            get
            {
                return item!=null?item.Columns:GetIntValue("Columns");
            }

            set
            {
                if (item != null)
                    item.Columns = value;
                else
                    SetValue("Columns", value);
               
            }
        }
        [DescriptionAttribute(ULabel.PROPERTY_TABLE_GroupIndex_DES)]
        public int GroupIndex
        {
            get
            {
                return item!=null?item.GroupIndex:GetIntValue("GroupIndex");
            }

            set
            {
                if (item != null)
                    item.GroupIndex = value;
                else
                    SetValue("GroupIndex", value);
                
            }
        }
        
        public int Height
        {
            get
            {
                return item!=null?item.Height:GetIntValue("Height");
            }

            set
            {
                if (item != null)
                    item.Height = value;
                else
                    SetValue("Height", value);
                
            }
        }


        [DescriptionAttribute(ULabel.PROPERTY_TABLE_TreeTable_DES)]
        public bool TreeTable
        {
            get
            {
                return item!=null?item.TreeTable:GetBoolValue("TreeTable");
            }

            set
            {
                if (item != null)
                    item.TreeTable = value;
                else
                    SetValue("TreeTable", value);
                if (DataChange != null)
                {
                    DataChange(this, new PropertyDataEvent("TreeTable", value));
                }
            }
        }
        [DescriptionAttribute(ULabel.PROPERTY_TABLE_TreeField_DES)]
        public string TreeField
        {
            get
            {
                return item!=null?item.TreeField: GetStringValue("TreeField");
            }

            set
            {
                if (item != null) item.TreeField = value;
                else
                    SetValue("TreeField", value);
                if (DataChange != null)
                {
                    DataChange(this, new PropertyDataEvent("TreeField", value));
                }
            }
        }
        [DescriptionAttribute(ULabel.PROPERTY_TABLE_TreeCaption_DES)]
        public string TreeCaption
        {
            get
            {
                return item!=null?item.TreeCaption:GetStringValue("TreeCaption");
            }

            set
            {
                if (item != null)
                {
                    item.TreeCaption = value;
                }
                else
                {
                    SetValue("TreeCaption", value);
                }
                if (DataChange != null)
                {
                    DataChange(this, new PropertyDataEvent("TreeCaption", value));
                }

            }
        }


        private object SetValue(string key,object value)
        {
            logger.Info("执行SetValue");
            if (this.array != null && this.array.Length > 0)
            {
                for (int i = 0; i < this.array.Length; i++)
                {
                    BillUiGroupToProperty data = array[i];
                    logger.Info(string.Format("修改字段[{0}]的[{1}]值为[{2}]",data.Caption,key,value));
                    Type ClazzType = typeof(BillUiGroupToProperty);
                    PropertyInfo Property = ClazzType.GetProperty(key);
                    if (Property != null)
                    {
                        Property.SetValue(data, value);
                        logger.Info("修改成功");
                    }
                }
            }
            return value;
        }

        private bool GetBoolValue(string key)
        {
            bool result = true;
            if (this.array != null && this.array.Length > 0)
            {
                for (int i = 0; i < this.array.Length; i++)
                {

                    BillUiGroupToProperty data = array[i];
                    Type ClazzType = typeof(BillUiGroupToProperty);
                    PropertyInfo Property = ClazzType.GetProperty(key);
                    if (Property != null && Property.GetMethod.ReturnType == typeof(bool))
                    {
                        object val = Property.GetValue(data);
                        if (val.Equals(false))
                        {
                            result = false;
                            break;
                        }
                    }
                }
            }

            return result;
        }

        private int GetIntValue(string key)
        {
            int result = 0;
            if (this.array != null && this.array.Length > 0)
            {
                BillUiGroupToProperty data = array[0];
                Type ClazzType = typeof(BillUiGroupToProperty);
                PropertyInfo Property = ClazzType.GetProperty(key);
                if (Property != null && Property.GetMethod.ReturnType == typeof(int))
                {
                    object val = Property.GetValue(data);
                    if(val is int)
                    {
                        result = (int)val;
                    }
                }
            }
            return result;
        }

        private string GetStringValue(string key)
        {
            string result = "";
            if (this.array != null && this.array.Length > 0)
            {
                for (int i = 0; i < this.array.Length; i++)
                {
                    BillUiGroupToProperty data = array[i];
                    Type ClazzType = typeof(BillUiGroupToProperty);
                    PropertyInfo Property = ClazzType.GetProperty(key);
                    if (Property != null && Property.GetMethod.ReturnType == typeof(string))
                    {
                        object val = Property.GetValue(data);
                        if (val is string)
                        {
                            if (result == "") result = val.ToString();
                            else if (result != (string)val)
                            {
                                result = "";
                                break;
                            }

                        }
                    }
                }  
            }
            return result;
        }


    }
}
